package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.dto.CodeManagerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.model.CodeManager;
import com.cqut.service.ICodeManagerService;
import com.cqut.util.ResponseUtil;
import com.cqut.util.StringUtil;


@RestController
@RequestMapping("/api/codeManager")
public class CodeManagerController {
    @Resource(name="codeManagerService")
    private ICodeManagerService codeManagerService;

    /**
     * 初始化码表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
    	@RequestParam("codeName")
    	String codeName,
    	@RequestParam("parentId")
    	String parentId,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();
        
        List<Map<String, Object>> codeManagerList = codeManagerService.getCodeList(codeName,parentId,limitShowDTO);
        
        long count = codeManagerService.getCodeListCount(parentId,codeName);

        result.put("rows", codeManagerList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }


     /**
     * 新增码表
     * @param codeManagerDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        CodeManagerDTO codeManagerDTO, BindingResult errors) {
    	
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        return ResponseUtil.getResEntityForGet(codeManagerService.addTableCode(codeManagerDTO));
    }
    
    /**
     * 根据ID更新码表
     * @param codeId
     * @param codeManagerDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        CodeManagerDTO codeManagerDTO, BindingResult errors) {
    	if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        CodeManager codeManagerModel = codeManagerDTO.toModel();
        return ResponseUtil.getResEntityForPPP(codeManagerService.update(codeManagerModel));
    }

    /**
	 * 判断码表名称是否重复(true为重复，false为不重复)
	 * */
	@RequestMapping(value = "/codeTableNameDC", method=RequestMethod.POST)
	public ResponseEntity<String> duplicateCheckingCodeName(
			String validationDate){
		
		if (StringUtil.isNullOrEmpty(validationDate))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		JSONObject json = new JSONObject();
		json.put("state", !codeManagerService.duplicateCheckingCodeName(validationDate));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
	
	/**
	 * 判断码表类型是否重复(true为重复，false为不重复)
	 * */
	@RequestMapping(value = "/codeTypeDC", method=RequestMethod.POST)
	public ResponseEntity<String> duplicateCheckingCodeType(
			String validationDate){
		
		if (StringUtil.isNullOrEmpty(validationDate))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		JSONObject json = new JSONObject();
		json.put("state", !codeManagerService.CheckingCodeType(validationDate));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
	
	/**
	 * 通过码表类型得到它所有的子数据
	 * */
	@RequestMapping(value = "/getCodeType", method=RequestMethod.GET)
	public ResponseEntity<String> findChildrenByParamName(
			@PathVariable("codeType")
			String codeType){
		if (StringUtil.isNullOrEmpty(codeType))
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		List<Map<String, Object>> codeTables = codeManagerService.findChildrenByCodeType(codeType);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(codeTables);
	}
	
	/**
	 * 通过Id数组删除码表
	 * */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteTableCode(
			@RequestParam("codeIds")
			String[] codeIds) {
		if(codeIds == null || 0 == codeIds.length)
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		if(codeManagerService.deleteTableCodesByParamCodes(codeIds))
			return ResponseUtil.getResEntityForPPPWhenSuccess();
		
		return ResponseUtil.getResEntityForPPPWhenFail();
	}
	

	/**
	 * 得到一条数据
	 * **/
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public ResponseEntity<String> getCodesByCodes( 
			@RequestParam("codeId")
			String codeId){
	    
			if(codeId == null)
				return ResponseUtil.getResponseEntityWhenInvalidReqParams();
	        Map<String, Object> result = codeManagerService.getDetailById(codeId);

	        return ResponseUtil.getResEntityForGetAndJson(result);
	}
}
